package com.yic.module.system.service.logger;

import com.yic.module.system.controller.admin.logger.vo.externallog.ExternalLogCreateReqVO;
import com.yic.module.system.controller.admin.logger.vo.externallog.ExternalLogExportReqVO;
import com.yic.module.system.controller.admin.logger.vo.externallog.ExternalLogPageReqVO;
import com.yic.module.system.controller.admin.logger.vo.externallog.ExternalLogUpdateReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.system.dal.dataobject.logger.ExternalLogDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.system.convert.logger.ExternalLogConvert;
import com.yic.module.system.dal.mysql.logger.ExternalLogMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.system.enums.ErrorCodeConstants.EXTERNAL_LOG_NOT_EXISTS;

/**
 * 第三方API调用日志 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ExternalLogServiceImpl implements ExternalLogService {

    @Resource
    private ExternalLogMapper externalLogMapper;

    @Override
    public Long createExternalLog(ExternalLogCreateReqVO createReqVO) {
        // 插入
        ExternalLogDO externalLog = ExternalLogConvert.INSTANCE.convert(createReqVO);
        externalLogMapper.insert(externalLog);
        // 返回
        return externalLog.getId();
    }

    @Override
    public void updateExternalLog(ExternalLogUpdateReqVO updateReqVO) {
        // 校验存在
        validateExternalLogExists(updateReqVO.getId());
        // 更新
        ExternalLogDO updateObj = ExternalLogConvert.INSTANCE.convert(updateReqVO);
        externalLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteExternalLog(Long id) {
        // 校验存在
        validateExternalLogExists(id);
        // 删除
        externalLogMapper.deleteById(id);
    }

    private void validateExternalLogExists(Long id) {
        if (externalLogMapper.selectById(id) == null) {
            throw exception(EXTERNAL_LOG_NOT_EXISTS);
        }
    }

    @Override
    public ExternalLogDO getExternalLog(Long id) {
        return externalLogMapper.selectById(id);
    }

    @Override
    public List<ExternalLogDO> getExternalLogList(Collection<Long> ids) {
        return externalLogMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ExternalLogDO> getExternalLogPage(ExternalLogPageReqVO pageReqVO) {
        return externalLogMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ExternalLogDO> getExternalLogList(ExternalLogExportReqVO exportReqVO) {
        return externalLogMapper.selectList(exportReqVO);
    }

}
